package tcpClient;
import info.clearthought.layout.TableLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ClientInterface extends javax.swing.JFrame implements ActionListener{
	private JLabel explanationLabel;
	private JLabel iPLabel;
	private JTextField IPTextField;
	private JButton enterButton;
	private JLabel infoLabel;
	private JButton connectButton;
	private JButton exitButton;
	private JTextField passwordTextField;
	private JTextField userNameTextField;
	private JLabel passwordLabel;
	private JLabel userNameLabel;
	
	private Client client;

        /**
        * Auto-generated main method to display this JFrame
        */
        public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                ClientInterface inst = new ClientInterface();
                                inst.setLocationRelativeTo(null);
                                inst.setVisible(true);
                        }
                });
        }
        
        public ClientInterface() {
                super();
                initGUI();
        }
        
        private void initGUI() {
                try {
                	TableLayout thisLayout = new TableLayout(new double[][] {{77.0, 81.0, 49.0, 74.0, 64.0, 62.0, TableLayout.FILL}, {46.0, 27.0, 35.0, 37.0, 36.0, TableLayout.FILL, 26.0, 6.0, 30.0, TableLayout.FILL}});
                	thisLayout.setHGap(5);
                	thisLayout.setVGap(5);
                	getContentPane().setLayout(thisLayout);
                        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                        {
                        	explanationLabel = new JLabel();
                        	getContentPane().add(explanationLabel, "1, 0, 4, 0, f, b");
                        	explanationLabel.setText("Introduce de IP of the server you want to connect:");
                        }
                        {
                        	iPLabel = new JLabel();
                        	getContentPane().add(iPLabel, "1, 2");
                        	iPLabel.setText("IP:");
                        }
                        {
                        	userNameLabel = new JLabel();
                        	getContentPane().add(userNameLabel, "1, 3");
                        	userNameLabel.setText("Username:");
                        }
                        {
                        	passwordLabel = new JLabel();
                        	getContentPane().add(passwordLabel, "1, 4");
                        	passwordLabel.setText("Password:");
                        }
                        {
                        	IPTextField = new JTextField();
                        	getContentPane().add(IPTextField, "2, 2, 3, 2");
                        	IPTextField.setText("127.0.0.1");
                        }
                        {
                        	userNameTextField = new JTextField();
                        	userNameTextField.setEnabled(false);
                        	getContentPane().add(userNameTextField, "2, 3, 3, 3");
                        }
                        {
                        	passwordTextField = new JPasswordField();
                        	passwordTextField.setEnabled(false);
                        	getContentPane().add(passwordTextField, "2, 4, 3, 4");                        }
                        {
                        	exitButton = new JButton();
                        	getContentPane().add(exitButton, "5, 8");
                        	exitButton.setText("Exit");
                        }
                        {
                        	connectButton = new JButton();
                        	connectButton.addActionListener(this);
                        	getContentPane().add(connectButton, "1, 8");
                        	connectButton.setText("Connect");
                        }
                        {
                        	infoLabel = new JLabel();
                        	getContentPane().add(infoLabel, "1, 6, 4, 6");
                        }
                        {
                        	enterButton = new JButton();
                        	enterButton.addActionListener(this);
                        	enterButton.setVisible(false);
                        	getContentPane().add(enterButton, "3, 8");
                        	enterButton.setText("Enter");
                        }
                        pack();
                        this.setSize(456, 323);
                } catch (Exception e) {
                    //add your error handling code here
                        e.printStackTrace();
                }
        }

		@Override
		public void actionPerformed(ActionEvent actionEvent) 
		{
			if (actionEvent.getSource() == connectButton)
			{
				System.out.println("connecting");
				client = new Client("127.0.0.1",6968);
				if (!client.getConnected())
				{
					infoLabel.setText("The connection failed. Try again.");
				}
				else
				{
					infoLabel.setText("Connected. Now insert user and password.");
					connectButton.setVisible(false);
					enterButton.setVisible(true);
					userNameTextField.setEnabled(true);
					passwordTextField.setEnabled(true);
				}
			}
			if (actionEvent.getSource() == enterButton)
			{
				client.validate(userNameTextField.getText(), passwordTextField.getText());
				if (client.getLogged())
				{
					infoLabel.setText("Wrong user or password. Please enter again");
				}
				else
				{
					infoLabel.setText("OK");
					this.setVisible(false);
					ClientInterface2 clientInterface = new ClientInterface2(client);
					clientInterface.setVisible(true);
				}
			}
		}
}